package com.jxbeidou.system.setting.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.AllArgsConstructor;
import com.jxbeidou.core.entity.PageResult;
import com.jxbeidou.core.mybatis.service.impl.BaseServiceImpl;
import com.jxbeidou.core.mybatis.convert.PageResultConvert;
import com.jxbeidou.system.setting.convert.ParamsConvert;
import com.jxbeidou.system.setting.entity.ParamsDO;
import com.jxbeidou.system.setting.query.ParamsQuery;
import com.jxbeidou.system.setting.vo.ParamsVO;
import com.jxbeidou.system.setting.mapper.ParamsMapper;
import com.jxbeidou.system.setting.service.ParamsService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 参数管理表
 *
 * @author 杜晓祥 
 * @since 1.0.0 2023-12-14
 */
@Service
@AllArgsConstructor
public class ParamsServiceImpl extends BaseServiceImpl<ParamsMapper, ParamsDO> implements ParamsService {

    @Override
    public PageResult<ParamsVO> page(ParamsQuery query) {
        IPage<ParamsDO> page = baseMapper.selectPage(getPage(query), getWrapper(query));
        PageResult<ParamsVO> result = PageResultConvert.convert(page, entityList -> ParamsConvert.INSTANCE.convertList(entityList));
        return result;
    }

    private LambdaQueryWrapper<ParamsDO> getWrapper(ParamsQuery query){
        LambdaQueryWrapper<ParamsDO> wrapper = Wrappers.lambdaQuery();
        wrapper.like(StringUtils.isNotEmpty(query.getParamName()), ParamsDO::getParamName, query.getParamName());
        wrapper.eq(query.getParamType() != null, ParamsDO::getParamType, query.getParamType());
        wrapper.eq(StringUtils.isNotEmpty(query.getParamKey()), ParamsDO::getParamKey, query.getParamKey());
        return wrapper;
    }

    @Override
    public void save(ParamsVO vo) {
        ParamsDO entity = ParamsConvert.INSTANCE.convert(vo);

        baseMapper.insert(entity);
    }

    @Override
    public void update(ParamsVO vo) {
        ParamsDO entity = ParamsConvert.INSTANCE.convert(vo);

        updateById(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(List<Long> idList) {
        removeByIds(idList);
    }

}